
package com.hlkj.pay.app.common.impl;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import org.springframework.stereotype.Service;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.pay.common.CommonResultCode;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.infrastructure.model.common.CommonAppDO;
import com.hlkj.pay.service.common.IBusinessLogService;
import com.hlkj.pay.service.common.ICommonAppConfigService;
import com.hlkj.pay.vo.common.req.CommonAppReq;

import cn.hutool.core.bean.BeanUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HlpayTeam
 * @date 2024/09/27 13:22
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CommonConfigAppServiceImpl implements com.hlkj.pay.app.common.ICommonConfigAppService {

    private final ICommonAppConfigService commonAppConfigService;
    private final IBusinessLogService businessLogService;
    @Override
    public CommonResult<String> addCommonApp(CommonAppReq addCommonAppReq) {
        log.info("addCommonApp addCommonAppReq:{}", JsonUtils.toJsonString(addCommonAppReq));
        try {
            // CommonAppQueryDto templateQueryDO = new CommonAppQueryDto();
            // templateQueryDO.setType(addCommonAppReq.getType());
            // templateQueryDO.setAppId(addCommonAppReq.getAppId());
            // List<CommonAppDO> commonAppList = commonAppConfigService.queryList(templateQueryDO);
            // if (!CollectionUtils.isEmpty(commonAppList)) {
            //     return CommonResult.error(CommonResultCode.COMMON_APP_EXISTS);
            // }
            CommonAppDO templateDto = BeanUtil.copyProperties(addCommonAppReq, CommonAppDO.class);
            commonAppConfigService.addCommonApp(templateDto);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("addCommonApp error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> updateCommonApp(CommonAppReq updateCommonAppReq) {
        log.info("updateCommonApp addCommonAppReq:{}", JsonUtils.toJsonString(updateCommonAppReq));
        try {
            CommonAppDO commonAppDO = commonAppConfigService.detail(updateCommonAppReq.getId());
            if (commonAppDO == null) {
                return CommonResult.error(CommonResultCode.COMMON_APP_NOT_EXIST);
            }
            
            // CommonAppQueryDto templateQueryDO = new CommonAppQueryDto();
            // templateQueryDO.setType(updateCommonAppReq.getType());
            // templateQueryDO.setAppId(updateCommonAppReq.getAppId());
            // List<CommonAppDO> commonAppList = commonAppConfigService.queryList(templateQueryDO);
            // if (!CollectionUtils.isEmpty(commonAppList)) {
            //     CommonAppDO commonApp = commonAppList.get(0);
            //     if (!commonApp.getId().equals(commonAppDO.getId())) {
            //         return CommonResult.error(CommonResultCode.TEMPLATE_NAME_ALREADY_EXISTS);
            //     }
            // }
            CommonAppDO appDO = BeanUtil.copyProperties(updateCommonAppReq, CommonAppDO.class);
            appDO.setId(updateCommonAppReq.getId());
            commonAppConfigService.updateCommonApp(appDO);
            // 变更日志
            businessChangeLog(commonAppDO, appDO);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("updateCommonApp error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> deleteCommonApp(Long id) {
        log.info("deleteCommonApp id:{}", id);
        try {
            CommonAppDO commonAppDO = commonAppConfigService.detail(id);
            if (commonAppDO == null) {
                return CommonResult.error(CommonResultCode.COMMON_APP_NOT_EXIST);
            }
            commonAppConfigService.deleteCommonApp(id);
            // 变更日志
            CommonAppDO afterValue = BeanUtil.copyProperties(commonAppDO, CommonAppDO.class);
            afterValue.setDeleteFlag(Boolean.TRUE);
            businessChangeLog(commonAppDO, afterValue);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("deleteCommonApp error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }

    @Override
    public CommonResult<Void> statusUpdate(Long id, CommonEnum.YES_NO_TYPE yesNoType) {
        log.info("statusUpdate id:{}, yesNoType:{}", id,yesNoType.getCode());
        try {
            CommonAppDO commonAppDO = commonAppConfigService.detail(id);
            if (commonAppDO == null) {
                return CommonResult.error(CommonResultCode.COMMON_APP_NOT_EXIST);
            }
            commonAppConfigService.statusUpdate(id, yesNoType);
            // 变更日志
            CommonAppDO afterValue = BeanUtil.copyProperties(commonAppDO, CommonAppDO.class);
            afterValue.setStatus(yesNoType.getCode());
            businessChangeLog(commonAppDO, afterValue);
            return CommonResult.success();
        }
        catch (Exception e) {
            log.error("statusUpdate error:{}", e.getMessage(), e);
            return CommonResult.error(CommonResultCode.SERVICE_ERROR);
        }
    }
    /**
     * log object
     *
     * @param beforePayChannel
     * @param afterPayChannel
     * @return
     */
    void businessChangeLog(CommonAppDO beforePayChannel, CommonAppDO afterPayChannel) {
        businessLogService.addBusinessChangeLog(beforePayChannel.getId().toString(), CommonEnum.CHANGE_LOG_TYPE.COMMON_APP.getCode(),
                CommonEnum.CHANGE_LOG_MODEL_TYPE.COMMON, beforePayChannel, afterPayChannel);
    }
    
}
